package com.sibbay.vta;

import java.io.File;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.sibbay.tools.Sharable;
import com.sibbay.tools.Tools;
import com.sibbay.vta.cache.Cache;

public class DayServiceListActivity extends CommonActivity {

	static final String EXTRA_ARRAY_ID = "ArrayId";
	static final String EXTRA_TITLE = "Title";
	static final String EXTRA_MAP_URL = "Map_Url";
	
	private ListView mList;

	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.service_list);
        
        final Intent intent = getIntent();
        final Sharable sh = intent.getParcelableExtra(EXTRA_ARRAY_ID);
        final Object[][] arrayId = (Object[][]) sh.obj();
        final String titleStr = intent.getStringExtra(EXTRA_TITLE);
        final String mapUrl = intent.getStringExtra(EXTRA_MAP_URL);
        
        final TextView title = (TextView) findViewById(R.id.title);
        title.setText(titleStr);
        
        mList = (ListView) findViewById(R.id.list);
        mList.setAdapter(new ArrListAdapter(this, arrayId));
        mList.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> l, View v, int pos, long id) {
		        final Intent intent = new Intent();
		        intent.setClassName(getPackageName(), RouteListActivity.class.getName());
		        intent.putExtra(RouteListActivity.EXTRA_TITLE, titleStr);
		        intent.putExtra(RouteListActivity.EXTRA_DAY, ((ArrListAdapter)l.getAdapter()).getDay(pos));
		        intent.putExtra(RouteListActivity.EXTRA_URL, ((ArrListAdapter)l.getAdapter()).getUrl(pos));
		        intent.putExtra(RouteListActivity.EXTRA_MAP_URL, mapUrl);
		        startActivity(intent);
//		        overridePendingTransition(R.anim.push_left_out, R.anim.push_right_in);
			}
		});
    }
	
	@Override
	protected void onResume() {
		super.onResume();
		
		mList.invalidateViews();
	}
	
	///
	
    private static final class ArrListAdapter extends BaseAdapter {
        
		private final Context mContext;
		private final Object[][] mArrayId;

        public ArrListAdapter(final Context context, final Object[][] arrayId) {
        	mContext = context;
			mArrayId = arrayId;
        }

        @Override
        public int getCount() {
            return mArrayId.length;
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            final ViewGroup vg;
            if (convertView == null) {
                vg = (ViewGroup) LayoutInflater.from(mContext).inflate(R.layout.day_service_list_item, parent, false);
//                final SVG svg = SVGParser.getSVGFromResource(mContext.getResources(), R.raw.saved);
//                ((ImageView) vg.findViewById(R.id.status)).setImageDrawable(svg.createPictureDrawable());
            } else {
                vg = (ViewGroup) convertView;
            }

            final TextView tv = (TextView) vg.findViewById(R.id.text);
            tv.setText((String)mArrayId[position][0]);
            
            if (Favorites.has(mContext, Tools.getKey(getUrl(position)))) {
            	vg.findViewById(R.id.in_favorites).setVisibility(View.VISIBLE);
            } else {
            	vg.findViewById(R.id.in_favorites).setVisibility(View.INVISIBLE);
            }
            
            if (Cache.getInstance(mContext).has(getUrl(position))) {
            	vg.findViewById(R.id.in_cache).setVisibility(View.VISIBLE);
            } else {
            	vg.findViewById(R.id.in_cache).setVisibility(View.INVISIBLE);
            }

            
            return vg;
        }
        
        ///

        String getDay(int position) {
        	return (String)mArrayId[position][0];
        }

        String getUrl(int position) {
        	return (String)mArrayId[position][1];
        }
    }
}
